package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.constant.MessageConstant;
import com.itheima.health.constant.RedisMessageConstant;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Member;
import com.itheima.health.service.MemberService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.Map;

/**
 * 用户登录
 *
 * @Author: HuChunping
 * @Date: 2021/01/17
 **/
@RestController
@RequestMapping("/login")
public class LoginController {
    private static final Logger log = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private JedisPool jedisPool;

    @Reference
    private MemberService memberService;

    @PostMapping("/check")
    public Result submit(@RequestBody Map<String, String> logInfo, HttpServletResponse response){
        // 从jedispool池中获取一个连接
        Jedis jedis = jedisPool.getResource();
        String telephone = logInfo.get("telephone");
        // 拼接该号码的jedis的key
        String key = RedisMessageConstant.SENDTYPE_LOGIN + "_" + telephone;
        // 该key在redis中的值
        String codeInRedis = jedis.get(key);
        log.info("codeInRedis:{}",codeInRedis);
        // 如果redis不存在这个key
        if (StringUtils.isEmpty(codeInRedis)){
            return new Result(false, "请重新获取验证码");
        }
        log.info("codeFromUI:{}",logInfo.get("validateCode"));
        // 存在key
        // 获取用户输入的验证码
        String validateCode = logInfo.get("validateCode");
        // 判断验证码是否正确
        // 如果验证码不正确
        if (!codeInRedis.equals(validateCode)){
            return new Result(false, MessageConstant.VALIDATECODE_ERROR);
        }
        // 验证码正确
        // 删除redis中的验证码,避免用户重复使用此验证码提交,(测试时可以不用)
        // jedis.del(key);
        jedis.close();
        // ======================判断是否为会员，不是注册，是：登录
        Member member = memberService.findByPhone(telephone);
        if (member==null) {
            // 注册会员
            Member newMember = new Member();
            newMember.setRegTime(new Date());
            newMember.setPhoneNumber(telephone);
            newMember.setRemark("快速登录");
            memberService.add(newMember);
        }
        Cookie cookie = new Cookie("login_member_telephone", telephone);
        cookie.setMaxAge(30*24*60*60);
        // 所有访问的url都带上这个cookie
        cookie.setPath("/");
        // 输出cookie
        response.addCookie(cookie);
        return new Result(true, MessageConstant.LOGIN_SUCCESS);
    }

    /**
     * 通过cookie获取手机号
     * @Author: HuChunping
     * @Date: 2021/1/18 11:28
     * @param telephone:
     * @return: com.itheima.health.entity.Result
     **/
    @GetMapping("/getTelephone")
    public Result getTelephone(@CookieValue("login_member_telephone") String telephone){
        return new Result(true, "获取手机号码成功", telephone);
    }
}
